package com.she.iiicoursecenter.view.tab2;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout.LayoutParams;

import com.she.iiicoursecenter.R;
import com.she.iiicoursecenter.activity.tab1_infomation.Tab1_CourseSet;
import com.she.iiicoursecenter.activity.tab1_infomation.Tab1_CourseSingle;
import com.she.iiicoursecenter.activity.tab1_infomation.Tab1_MainActivity;
import com.she.iiicoursecenter.activity.tab2_time.Tab2_CourseSelectedList;
import com.she.iiicoursecenter.activity.tab2_time.Tab2_MainActivity;
import com.she.iiicoursecenter.activity.tab3_reservation.Tab3_MainActivity;
import com.she.iiicoursecenter.adapter.Tab2_Adapter;
import com.she.iiicoursecenter.adapter.Tab3_Adapter;
import com.she.iiicoursecenter.bean.CourseSingleBundleBean;
import com.she.iiicoursecenter.bean.ViewChageDataBean;
import com.she.iiicoursecenter.event.connect.bean.SearchContentBean;
import com.she.iiicoursecenter.view.template.CustomizedView;

public class Tab2_CourseSelectedListView extends CustomizedView implements OnClickListener {

	private ListView mListView;
	private Tab2_Adapter mAdapter;
	private List<SearchContentBean> data;

	// test only!!!
	private ImageView mImgBack;

	public Tab2_CourseSelectedListView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	private void init() {
		//
		LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.tab2_courseselectedlist, this, true);
		//

		// test only!!!
		mImgBack = (ImageView) this.findViewById(R.id.img_back);
		mImgBack.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = Tab2_CourseSelectedListView.this.getApp().mChangeViewHandler.obtainMessage();
				msg.what = Tab2_CourseSelectedListView.this.getApp().MSG_WHAT_CHANGEVIEW;
				ViewChageDataBean dataBean = new ViewChageDataBean(Tab2_CourseSelectedList.class, Tab2_MainActivity.class, null);
				msg.obj = dataBean;
				Tab2_CourseSelectedListView.this.getApp().mChangeViewHandler.sendMessage(msg);
				getApp().fadeout = true;
			}
		});
		
		data = getApp().getSearchContentData();
		mAdapter = new Tab2_Adapter(getContext(), data);
		mListView = (ListView) this.findViewById(R.id.tab2_listview);
		mListView.setDivider(new ColorDrawable(Color.TRANSPARENT));// 去除listview分隔線

		LinearLayout layout = new LinearLayout(getContext());
		ImageView footer2 = new ImageView(getContext());
		footer2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		footer2.setImageResource(R.drawable.footer_img);
		layout.addView(footer2);
		mListView.addFooterView(layout);

		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				//避免點到footer
				if (data.size() > 0) {
					if (arg2 < data.size()) {
						Message msg = Tab2_CourseSelectedListView.this.getApp().mChangeViewHandler.obtainMessage();
						msg.what = Tab2_CourseSelectedListView.this.getApp().MSG_WHAT_CHANGEVIEW;
						CourseSingleBundleBean bundleBean = new CourseSingleBundleBean( data.get(arg2).getContentId(), data.get(arg2).getContentName());
						ViewChageDataBean dataBean = new ViewChageDataBean(Tab2_CourseSelectedList.class, Tab1_CourseSingle.class, bundleBean);
						msg.obj = dataBean;
						Tab2_CourseSelectedListView.this.getApp().mChangeViewHandler.sendMessage(msg);
						Tab2_CourseSelectedListView.this.getApp().tab2InCourseSingle = true;
						Tab2_CourseSelectedListView.this.getApp().tab3InCourseSingle = false;
					}
				}
			}
		});

	}

	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	public void notifyAdapter(){
		mAdapter.notifyDataSetChanged();
	}
}
